package com.example.ad_project;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

import com.nus.iss.ad.dbhelper.DiscrepancyDBHelper;
import com.nus.iss.ad.model.Inventory;

public class RaiseDiscrepancyActivity extends BaseActivity implements OnClickListener {
	
		DiscrepancyDBHelper data=new DiscrepancyDBHelper();
		List<Inventory> invList=null;
		AutoCompleteTextView tv;
		TextView message;
	
		final static int Raise_DISCREPANCY_DETAIL_REQUEST = 908;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_raise_discrepancy);
		
		tv=(AutoCompleteTextView)findViewById(R.id.actvItemCode);
		Button b=(Button)findViewById(R.id.btnSearch);
		b.setOnClickListener(this);
		
		message=(TextView)findViewById(R.id.textViewMessage);
		message.setVisibility(View.GONE);
		
		invList=data.getAllInventory();
		
		String[] resultArr=new String[invList.size()];
		for(int i=0;i<resultArr.length;i++)
		{
			Inventory inv=invList.get(i);
			resultArr[i]=inv.getInventoryId();
		}
		ArrayAdapter<String> adapter = 
		        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);
		tv.setThreshold(1);
		tv.setAdapter(adapter);
		//adapter.notifyDataSetChanged();  
	
	}

	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String code=tv.getText().toString();
		Inventory inv=data.getInventoryById(code);
		if(inv==null || inv.getInventoryId()==null){
			message.setVisibility(View.VISIBLE);
		}
		else{
			message.setVisibility(View.GONE);
			Intent i=new Intent(this,RaiseDiscrepancyDetailActivity.class);
			i.putExtra("code", code);
			startActivityForResult(i, Raise_DISCREPANCY_DETAIL_REQUEST);
		}
	}

	protected void onActivityResult
    (int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == Raise_DISCREPANCY_DETAIL_REQUEST) {
//refresh by restarting
recreate();
		}
    }
}